package com.hy.server;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * author: houying
 * date  : 17-6-8
 * desc  :
 */
public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, ServerChannelContext> {

    @Override
    public void completed(AsynchronousSocketChannel result, ServerChannelContext serverChannelContext) {
        AsynchronousServerSocketChannel serverSocketChannel = serverChannelContext.getServerSocketChannel();
        serverSocketChannel.accept(serverChannelContext, this);

        Session session = new Session(result);
        ByteBuffer byteBuffer = session.getReadByteBuffer();
        result.read(byteBuffer, session, serverChannelContext.getReadCompletionHandler());
    }

    @Override
    public void failed(Throwable exc, ServerChannelContext attachment) {

    }
}
